package com.meilai.project.service.business.customer.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.dto.business.customer.CustomerFollowTmpDTO;
import com.meilai.project.entity.business.customer.CustomerFollowAdvanceTmp;
import com.meilai.project.entity.business.customer.CustomerFollowPhotoTmp;
import com.meilai.project.entity.business.customer.CustomerFollowTmp;
import com.meilai.project.entity.business.customer.CustomerRecommendedResourcesTmp;
import com.meilai.project.mapper.business.customer.CustomerFollowTmpMapper;
import com.meilai.project.service.business.customer.CustomerFollowAdvanceTmpService;
import com.meilai.project.service.business.customer.CustomerFollowPhotoTmpService;
import com.meilai.project.service.business.customer.CustomerFollowTmpService;
import com.meilai.project.service.business.customer.CustomerRecommendedResourcesTmpService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户跟进临时表 服务实现类
 * </p>
 *
 * @author xhy
 * @since 2022-01-06
 */
@Service
public class CustomerFollowTmpImpl extends ServiceImpl<CustomerFollowTmpMapper, CustomerFollowTmp> implements CustomerFollowTmpService {

    @Autowired
    private CustomerRecommendedResourcesTmpService customerRecommendedResourcesTmpService;

    @Autowired
    private CustomerFollowAdvanceTmpService customerFollowAdvanceTmpService;

    @Autowired
    private CustomerFollowPhotoTmpService customerFollowPhotoTmpService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveBatch(List<CustomerFollowTmpDTO> dtos, Long flowId) {
        boolean flag = true;
        List<CustomerFollowTmp> list = this.list(Wrappers.<CustomerFollowTmp>lambdaQuery().eq(CustomerFollowTmp::getCustomer_info_flow_id, flowId).isNull(CustomerFollowTmp::getDeleted_at));
        if (list.size() > 0) {
            Set<Long> collect = list.stream().map(CustomerFollowTmp::getId).collect(Collectors.toSet());
            this.remove(Wrappers.<CustomerFollowTmp>lambdaQuery().in(CustomerFollowTmp::getId, collect));
            customerFollowAdvanceTmpService.remove(Wrappers.<CustomerFollowAdvanceTmp>lambdaQuery().in(CustomerFollowAdvanceTmp::getCustomer_follow_tmp_id, collect));
            customerRecommendedResourcesTmpService.remove(Wrappers.<CustomerRecommendedResourcesTmp>lambdaQuery().in(CustomerRecommendedResourcesTmp::getCustomer_follow_tmp_id, collect));
            customerFollowPhotoTmpService.remove(Wrappers.<CustomerFollowPhotoTmp>lambdaQuery().in(CustomerFollowPhotoTmp::getCustomer_follow_tmp_id, collect));
        }
        for (CustomerFollowTmpDTO dto : dtos) {
            CustomerFollowTmp customerFollowTmp = new CustomerFollowTmp();
            BeanUtils.copyProperties(dto, customerFollowTmp);
            customerFollowTmp.setCustomer_info_flow_id(flowId);
            this.save(customerFollowTmp);
            customerRecommendedResourcesTmpService.saveBatch(dto.getResource_list().stream().map(item -> {
                CustomerRecommendedResourcesTmp customerRecommendedResourcesTmp = new CustomerRecommendedResourcesTmp();
                customerRecommendedResourcesTmp.setResources_id(item);
                customerRecommendedResourcesTmp.setCustomer_follow_tmp_id(customerFollowTmp.getId());
                return customerRecommendedResourcesTmp;
            }).collect(Collectors.toList()));
            customerFollowAdvanceTmpService.saveBatch(dto.getAction_list().stream().map(item -> {
                CustomerFollowAdvanceTmp customerFollowAdvanceTmp = new CustomerFollowAdvanceTmp();
                customerFollowAdvanceTmp.setAdvance_id(item);
                customerFollowAdvanceTmp.setCustomer_follow_tmp_id(customerFollowTmp.getId());
                return customerFollowAdvanceTmp;
            }).collect(Collectors.toList()));
            // todo 新增逻辑 保存上传图片
            if (dto.getUrl_list() != null) {
                customerFollowPhotoTmpService.saveBatch(dto.getUrl_list().stream().map(url -> {
                    CustomerFollowPhotoTmp tmp = new CustomerFollowPhotoTmp();
                    tmp.setCustomer_follow_tmp_id(customerFollowTmp.getId());
                    tmp.setUrl(url);
                    return tmp;
                }).collect(Collectors.toList()));
            }
        }
        return flag;
    }

    @Override
    public Page<CustomerFollowTmpDTO> page(Integer current_page, Integer page_size, Long flow_id) {
        Page<CustomerFollowTmpDTO> page = new Page<>(current_page, page_size);
        List<CustomerFollowTmpDTO> list = baseMapper.page(page, flow_id);
        if (CollectionUtils.isNotEmpty(list)) {
            List<Long> ids = list.stream().map(CustomerFollowTmpDTO::getId).collect(Collectors.toList());
            Map<Long, List<CustomerFollowPhotoTmp>> map = customerFollowPhotoTmpService.list(Wrappers.<CustomerFollowPhotoTmp>lambdaQuery().in(CustomerFollowPhotoTmp::getCustomer_follow_tmp_id, ids)).stream().collect(Collectors.groupingBy(CustomerFollowPhotoTmp::getCustomer_follow_tmp_id));
            list.forEach(item -> {
                if (map.containsKey(item.getId())) {
                    item.setUrl_list(map.get(item.getId()).stream().map(CustomerFollowPhotoTmp::getUrl).collect(Collectors.toList()));
                } else {
                    item.setUrl_list(new ArrayList<>());
                }
            });
        }
        page.setRecords(list);
        return page;
    }
}
